<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-01 13:44:13
 * @Last Modified time: 2023-02-21 15:56:39
 */
namespace app\api\controller\user;	
use think\facade\App;
use app\common\services\user\UserService;
use app\common\services\store\OrderService;
use app\common\services\store\OrderRefundService;

/**
 * Class User
 */
class User extends \app\api\controller\Base
{ 	
    /**
     * UserService constructor.
     * @param App $app
     */
    public function __construct(App $app,UserService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 获取用户详细
     * @force(false)
     * @method(GET)
     */
    public function detail(){
        $orderService = OrderService::instance();
        $orderFilter = ['user_id'=>$this->uid];
        $this->success('获取成功',[
            'user'=>$this->user,
            'order_count' => [
                'payment' => $orderService->getCount($orderFilter, 'payment'),
                'delivery' => $orderService->getCount($orderFilter, 'delivery'),
                'received' => $orderService->getCount($orderFilter, 'received'),
                'comment' => $orderService->getCount($orderFilter, 'comment'),
                'refund' => OrderRefundService::instance()->getCount([
                    'user_id'=>$this->uid,
                    'status'=>[1,3]
                ]),
            ],
        ]);
    }

    /**
     * 修改用户信息
     * @method(PUT)
     */
    public function update(){
        $data = [];
        if(!empty($avatar = $this->request->param('avatar'))){
            $data['avatar'] = $avatar;
        }
        if(!empty($nickName = $this->request->param('nickName'))){
            $data['nickName'] = $nickName;
        }
        if($this->service->update($this->uid, $data)){
            $this->success('保存成功!',$this->service->getDetail([
                'id'=>$this->uid
            ]));
        }
        $this->error('保存失败!');
    }
}